package com.gxa.ssm340.service.impl;

import com.gxa.ssm340.common.ErrorCode;
import com.gxa.ssm340.entity.dto.UserPageDTO;
import com.gxa.ssm340.entity.pojo.Points;
import com.gxa.ssm340.entity.pojo.User;
import com.gxa.ssm340.entity.vo.UsersAndPointsCountVO;
import com.gxa.ssm340.entity.vo.UsersAndPointsVO;
import com.gxa.ssm340.exception.BizException;
import com.gxa.ssm340.mapper.UserMapper;
import com.gxa.ssm340.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 赵文川
 * @Description: $
 * @date 2023/10/23 19:30
 */
@Service
@Transactional(rollbackFor = Throwable.class)
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UsersAndPointsCountVO getUsersAndPoints(UserPageDTO dto) throws BizException {
        if(dto == null) {
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        // 创建用户积分VO集合对象
        UsersAndPointsCountVO usersAndPointsCountVO = new UsersAndPointsCountVO();
        List<UsersAndPointsVO> usersAndPointsVOS = new ArrayList<>();
        // 获取用户信息
        List<User> users = userMapper.selectAll(dto);
        for (User user : users) {
            // 创建用户积分VO对象
            UsersAndPointsVO usersAndPointsVO = new UsersAndPointsVO();
            Integer totalPoint = userMapper.selectPointByPhone(user.getPhone());
            if(totalPoint != null) {
                usersAndPointsVO.setTotalPoint(totalPoint);
            }else {
                usersAndPointsVO.setTotalPoint(0);
            }
            usersAndPointsVO.setUserId(user.getUserId());
            usersAndPointsVO.setName(user.getName());
            usersAndPointsVO.setPhone(user.getPhone());
            usersAndPointsVO.setRegTime(user.getRegTime());
            usersAndPointsVOS.add(usersAndPointsVO);
        }
        usersAndPointsCountVO.setUsersAndPointsVOS(usersAndPointsVOS);
        List<User> list = userMapper.selectAll(dto);
        usersAndPointsCountVO.setCount(list.size());
        if(users == null) {
            throw new BizException(ErrorCode.NULL_USER);
        }
        return usersAndPointsCountVO;
    }

    @Override
    public List<Points> getPointDetail(UserPageDTO dto,String flag) throws BizException {
        if(dto.getField() == null || dto.getField() == "") {
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        List<Points> pointsList = null;
        if(flag == "in"){
            pointsList = userMapper.selectInPointDetailByPhone(dto.getField());
        }else if(flag == "out"){
            pointsList = userMapper.selectOutPointDetailByPhone(dto.getField());
        }
        if(pointsList == null) {
            throw new BizException(ErrorCode.NULL_USER);
        }
        return pointsList;
    }

    @Override
    public List<Points> getPointDetail(UserPageDTO dto) throws BizException {
        if(dto.getField() == null || dto.getField() == "") {
            throw new BizException(ErrorCode.NULL_FIELD);
        }
        List<Points> pointsList = userMapper.selectPointDetailByPhone(dto.getField());
        if(pointsList == null) {
            throw new BizException(ErrorCode.NULL_USER);
        }
        return pointsList;
    }
}
